local skynet = require "skynet"
local handler = require "handler"
local error_code = require "error_code"
local constant = require "constant"
local cjson = require "cjson"

local table_insert = table.insert

local club_handler = {}

function club_handler.REQ_SELF_CLUB_INFO(msg,client_fd)
    local uid = player.uid
    local ok,club_id,vip_level = R().basesvr({key=uid}):call('.msg_handler','get_clubid_and_viplevel',uid)
    if not ok then
        send_to_gateway('club.RSP_SELF_CLUB_INFO', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    print("REQ_SELF_CLUB_INFO================",club_id,vip_level)
    if (club_id or 0) <= 0 then --没加入俱乐部
        local is_white_player = false
        local ok,ret = R().hallsvr(1):call('.msg_handler','common_post_data',constant.WHITE_PLAYER_URL,{channel = player.channel},uid)
        if ok and ret and ret.status == 200 and ret.body then
            local body = cjson.decode(ret.body)
            is_white_player = body.code == 200 and true or false
        end

        --白名单或者vip等级>=5 可以创建 否则只能加入
        local status = (is_white_player or vip_level >= 5) and 1 or 2
        return send_to_gateway('club.RSP_SELF_CLUB_INFO', {status = status})
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_data',uid,club_id)
    if not ok or not rsp then
        return send_to_gateway('club.RSP_SELF_CLUB_INFO', {result = -1})
    end
    send_to_gateway('club.RSP_SELF_CLUB_INFO', {status = 3,info = rsp.data})

    return true
end

function club_handler.REQ_CREATE_CLUB(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CREATE_CLUB', {result = -1})
        return
    end

    --已经加入俱乐部 不可以创建俱乐部
    if club_id > 0 then
        send_to_gateway('club.RSP_CREATE_CLUB', {result = error_code.HAS_CLUB_CANT_CREATE})
        return
    end

    local divide_rate = (global_configs.value or {}).CLUB_DEFAULT_RATE or 20
    local create_data = {
        uid = player.uid,
        icon = msg.icon,
        name = msg.name,
        notice = msg.notice,
        max_number = constant.CLUB_MAX_NUMBER,
        apply_way = constant.CLUB_AUDIT_WAY.NEED_AUDIT,
        divide_rate = divide_rate,
        creator_name = player.user_data.name,
    }

    --记得判断敏感词
    if skynet.call('.textfilter','lua','is_sensitive',msg.name) then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.INVALID_NAME})
    end

    --记得判断敏感词
    if skynet.call('.textfilter','lua','is_sensitive',msg.notice) then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.INVALID_NOTICE})
    end

    --判断名字是否存在
    local ok,is_exit = R().clubsvr(1):call('.club_mgr','is_club_name_exist',msg.name)
    if is_exit then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.CLUB_NAME_EXIST})
    end
    
    --计算名字总长度
    local name_map = skynet.call('.textfilter','lua','get_utf8_length',msg.name)
    local total_len = 0
    for _,wlen in pairs(name_map) do
        if wlen == 1 then
            total_len = total_len + 1
        else
            total_len = total_len + 2
        end
    end

    if total_len < 1 then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.CHANGE_NAME_EMPTY})
    end
    if total_len > 30 then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.CLUB_NAME_TOO_LONG})
    end

    --计算公告长度
    local name_map = skynet.call('.textfilter','lua','get_utf8_length',msg.notice)
    local total_len = 0
    for _,wlen in pairs(name_map) do
        if wlen == 1 then
            total_len = total_len + 1
        else
            total_len = total_len + 2
        end
    end

    if total_len > 75 then
        return send_to_gateway('club.RSP_CREATE_CLUB',{result = error_code.CLUB_NAME_TOO_LONG})
    end

    local ok,club_data = R().clubsvr(1):call('.club_mgr','create_club',create_data)
    if not ok or not club_data then
        send_to_gateway('club.RSP_CREATE_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_CREATE_CLUB',{info = club_data})

    return true
end

function club_handler.REQ_DISMISS_CLUB(msg,client_fd)
    local uid = player.uid
    local ok, ret = R().clubsvr(1):call('.club_mgr','dismiss_club',uid,msg.club_id)
    if not ok or not ret then
        errlog(uid, 'failed to call clubsvr.dismiss_club')
        send_to_gateway('club.RSP_DISMISS_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_DISMISS_CLUB',{club_id = msg.club_id})

    return true
end

function club_handler.REQ_REMOVE_CLUB_MEMBER(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_REVOKE_CLUB_ADMIN', {result = -1})
        return
    end

    local ok, rsp = R().clubsvr(1):call('.club_mgr','remove_club_member',uid,club_id,msg.target_uid)
    if not ok then
        errlog(uid, 'failed to call clubsvr.remove_club_member')
        send_to_gateway('club.RSP_REMOVE_CLUB_MEMBER', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    if rsp and rsp.result then
        send_to_gateway('club.RSP_REMOVE_CLUB_MEMBER', rsp)
        return
    end
    
    send_to_gateway('club.RSP_REMOVE_CLUB_MEMBER', {target_uid = msg.target_uid})

    return true
end

function club_handler.REQ_APPLY_EXIT_CLUB(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_REVOKE_CLUB_ADMIN', {result = -1})
        return
    end

    local ok, rsp = R().clubsvr(1):call('.club_mgr','apply_exit_club',uid,club_id)
    if not ok then
        errlog(uid, 'failed to call clubsvr.apply_exit_club')
        send_to_gateway('club.RSP_APPLY_EXIT_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_APPLY_EXIT_CLUB', rsp)

    return true
end

function club_handler.REQ_SET_CLUB_INFO(msg,club_id)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_REVOKE_CLUB_ADMIN', {result = -1})
        return
    end

    --记得判断敏感词
    if skynet.call('.textfilter','lua','is_sensitive',msg.notice) then
        return send_to_gateway('club.RSP_SET_CLUB_INFO',{result = error_code.INVALID_NOTICE})
    end

    local data = {notice = msg.notice,icon = msg.icon,apply_way = msg.apply_way}
    local ok,rsp = R().clubsvr(1):call('.club_mgr','set_club_info',uid,club_id,data)
    if not ok then
        send_to_gateway('club.RSP_SET_CLUB_INFO', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    if rsp and rsp.result then
        send_to_gateway('club.RSP_SET_CLUB_INFO',rsp)
        return
    end

    send_to_gateway('club.RSP_SET_CLUB_INFO',data)

    return true
end

function club_handler.REQ_GET_CLUB_DATA(msg, client_fd)
    local uid = player.uid
    local ok, rsp = R().clubsvr({key=uid}):call('.club_mgr','get_club_data',uid,msg.club_id)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_data')
        send_to_gateway('club.RSP_GET_CLUB_DATA', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_GET_CLUB_DATA',{info = rsp.data})

    return true
end

--获取成员列表
function club_handler.REQ_GET_MEMBER_LIST(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_GET_MEMBER_LIST', {result = -1})
        return
    end

    local ok, rsp = R().clubsvr(1):call('.club_mgr', 'get_member_list',uid,club_id)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_member_list')
        send_to_gateway('club.RSP_GET_MEMBER_LIST', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_GET_MEMBER_LIST',rsp)

    return true
end

--申请加入俱乐部
function club_handler.REQ_APPLY_JOIN_CLUB(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if  ok and club_id and club_id ~= 0 then
        send_to_gateway('club.RSP_APPLY_JOIN_CLUB', {result = error_code.PLAYER_HAS_CLUB})
        return
    end

    local ok, rsp = R().clubsvr(1):call('.club_mgr', 'apply_join_club', uid,msg.club_id)
    if not ok then
        errlog(uid, 'failed to call clubsvr.apply_join_club')
        send_to_gateway('club.RSP_APPLY_JOIN_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    send_to_gateway('club.RSP_APPLY_JOIN_CLUB',rsp)

    return true
end

--同意或者拒绝加入俱乐部
function club_handler.REQ_AUDIT_JOIN_CLUB(msg, client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_AUDIT_JOIN_CLUB', {result = -1})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','audit_join_club',uid,club_id,msg.target_uid,msg.agree)
    if not ok then
        errlog(uid, 'failed to call clubsvr.audit_join_club')
        send_to_gateway('club.RSP_AUDIT_JOIN_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    send_to_gateway('club.RSP_AUDIT_JOIN_CLUB', rsp)

    return true
end

function club_handler.REQ_CLUB_RANK(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CLUB_RANK', {result = -1})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_rank',uid,club_id,msg.rank_type)
    if not ok then
        errlog(uid, 'failed to call clubsvr.REQ_CLUB_RANK')
        send_to_gateway('club.RSP_CLUB_RANK', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    send_to_gateway('club.RSP_CLUB_RANK', rsp)

    return true
end

function club_handler.REQ_CLUB_MESSAGE(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CLUB_MESSAGE', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_message',uid,club_id)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_message')
        send_to_gateway('club.RSP_AUDIT_JOIN_CLUB', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_CLUB_MESSAGE', {message_list = rsp})

    return true
end

function club_handler.REQ_CLUB_INCOME(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CLUB_INCOME', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_income',uid,club_id)
    print("REQ_CLUB_INCOME=================",tostring_r(rsp))
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_income')
        send_to_gateway('club.RSP_CLUB_INCOME', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_CLUB_INCOME', rsp)

    return true
end

function club_handler.REQ_CLUB_EXCHANGE(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CLUB_EXCHANGE', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    if msg.coins < 1 then
        send_to_gateway('club.RSP_CLUB_EXCHANGE', {result = error_code.CLUB_EXCHANGE_INVALID})
        return 
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','club_exchange',uid,club_id,msg.coins)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_exchange')
        send_to_gateway('club.RSP_CLUB_EXCHANGE', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_CLUB_EXCHANGE',rsp)

    return true
end

function club_handler.REQ_GET_CLUB_TABLE_CONF(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_GET_CLUB_TABLE_CONF', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    local conf_list = {}
    local roomdata = global_configs.clubroomdata or {}
    for k,v in pairs(roomdata) do
        if v.open_game then
            local data = {
                id = k,
                game_type = v.table_type,
                dizhu = v.dizhu,
                dingzhu = v.dingzhu,
                min = v.min,
                min_compare_round = v.max_bet_round,
                max_compare_round = v.comparable_bet_round,
                min_look_round = v.max_look_round,
                max = v.max,
            }
            table_insert(conf_list,data)
        end
    end
    send_to_gateway('club.RSP_GET_CLUB_TABLE_CONF', {conf_list = conf_list})

    return true
end

function club_handler.REQ_CLUB_RECORD(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_CLUB_RECORD', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_record',club_id,uid,msg.record_type)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_record')
        send_to_gateway('club.RSP_CLUB_RECORD', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_CLUB_RECORD',rsp)

    return true
end

function club_handler.REQ_NOTICE_INFO(msg,client_fd)
    local uid = player.uid
    local ok,club_id = R().basesvr({key=uid}):call('.msg_handler','get_club_id',uid)
    if not ok or not club_id then
        send_to_gateway('club.RSP_NOTICE_INFO', {result = error_code.REMOTE_CALL_FAIL})
        return
    end

    local ok,rsp = R().clubsvr(1):call('.club_mgr','get_club_notice_info',club_id,uid)
    if not ok then
        errlog(uid, 'failed to call clubsvr.get_club_notice_info')
        send_to_gateway('club.RSP_NOTICE_INFO', {result = error_code.REMOTE_CALL_FAIL})
        return
    end
    send_to_gateway('club.RSP_NOTICE_INFO',rsp)

    return true
end

--------------------------------------------------------
handler.register_module_handlers('club', club_handler)
